package com.erytech.linktalk.web.server;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.erytech.linktalk.dao.domain.User;
import com.erytech.linktalk.service.ServiceFacade;

public class SessionListener implements HttpSessionListener {

	Log logger = LogFactory.getLog(SessionListener.class);
	
	public void sessionCreated(HttpSessionEvent se) {
		logger.debug("Session created: " + se);
		logger.debug("ServletContext: " + se.getSession().getServletContext());
	}

	public void sessionDestroyed(HttpSessionEvent se) {
		logger.debug("Session destoyed: " + se);
		HttpSession session = se.getSession();
		User user = (User)session.getAttribute("user");
		if(user != null) {
			logger.debug("Logout: " + user);
			ServiceFacade serviceFacade = (ServiceFacade)session.getAttribute("serviceFacade");
			serviceFacade.getUserManager().logout(user);
		}
	}

}